package Long.Lu.Controller;

import Long.Lu.Service.Impl.DeployServiceImpl;
import Long.Lu.pojo.Deploy;
import Long.Lu.pojo.DeployParam;
import Long.Lu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ty
 * @createDate: 2020/10/25
 * @version: 1.0
 */
@Controller
public class DeploymentController {
    @Autowired
    private DeployServiceImpl deployServiceImpl;

    @Autowired
    JavaMailSenderImpl mailSender;

    //查询所有调配记录
    @GetMapping("/deploy/list")
    public String toDeployList(Model model) {
        List<Deploy> deploys = deployServiceImpl.SelectAllDeploy();
        model.addAttribute("list",deploys);
        System.out.println(deploys);
        return "deploy/deploy-list";
    }

    //跳转添加员工界面
    @GetMapping("/deploy/add")
    public String addDeploy() {
//        List<Deploy> deploys = deployServiceImpl.SelectAllDeploy();
//        map.put("deploy",deploys);
        return "deploy/deploy-add";
    }

    //添加一个员工
    @RequestMapping("/deploy/save/{path}")
    @ResponseBody
    public boolean insetDeploy(@RequestBody DeployParam deployParam, @PathVariable("path") String path) {
        System.out.println(deployParam+"---------------");
         if (path.equals("add")){
             return deployServiceImpl.addDeploy(deployParam);
         }else if (path.equals("update")){
             return deployServiceImpl.updateDeploy(deployParam);
         }
         return false;
    }

    //编辑 /userInfo/update
    @RequestMapping("/deploy/update")
    public String update(Map<String,Object> map,@RequestParam("deployId") Integer deployId){

        Deploy deploy = deployServiceImpl.updateById(deployId);
        map.put("deploy",deploy);
        return "deploy/deploy-update";
    }

    //修改成功跳转
//    @RequestMapping("/deploy/save/update")
//    @ResponseBody
//    public boolean updateSuccess(DeployParam deployParam){
//        return deployServiceImpl.updateDeploy(deployParam);
//    }


    //查看一个员工基本信息/deploy/show
    @RequestMapping("/deploy/show")
    public String show(Map<String,Object> map,@RequestParam("employeeId") Integer employeeId){
        System.out.println(employeeId+"=================");
        Employee employee = deployServiceImpl.employeeById(employeeId);
        System.out.println(employee+"-------------------");
        map.put("employee",employee);
        return "deploy/employee-show";
    }

    //删除一个人事调配记录
    @RequestMapping("/deploy/delDeploy")
    @ResponseBody
    public boolean delDeploy(@RequestParam("deployId") Integer deployId){
        System.out.println(deployId+"<===deployId");
        return deployServiceImpl.delDeploy(deployId);
    }

    //批量删除 /deploy/delAll
    @RequestMapping("/deploy/delAll")
    @ResponseBody
    public boolean delAll (@RequestParam("deployIds") String deployIds){
        return deployServiceImpl.delDeployAll(deployIds);
    }

    //excel导出
    @RequestMapping("/deploy/excelWrite")
    public void excelWrite(HttpServletResponse response){
        System.out.println("response==>"+response);
        deployServiceImpl.excelWrite(response);
    }

    //邮件发送/deploy/send
    @RequestMapping("/deploy/send")
    @ResponseBody
    public Map<String,Object> send(){
        Map<String,Object> map = new HashMap<>();
        //一个简单的邮件
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("您好，贝壳人力资源人事通知！！！");
            mailMessage.setText("您有新的邮件轻注意查收！！！");
            mailMessage.setTo("2911209994@qq.com");
            mailMessage.setFrom("2911209994@qq.com");
            mailSender.send(mailMessage);
            map.put("code",0);
            return map;
        } catch (MailException e) {
            e.printStackTrace();
        }
        map.put("code",-1);
        return map;
    }

    //展示合同信息
    @RequestMapping("/deployment/contract-content")
    public String content(){
        return "deployment/archives-content";
    }

    ///contract/send
    @RequestMapping("/contract/send")
    @ResponseBody
    public Map<String,Object> sendMail(){
        Map<String,Object> map = new HashMap<>();
        //一个简单的邮件
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("您好，悠悠人力资源人事通知！！！");
            mailMessage.setText("您有心的邮件轻注意查收！！！");
            mailMessage.setTo("2911209994@qq.com");
            mailMessage.setFrom("2911209994@qq.com");
            mailSender.send(mailMessage);
            map.put("code",0);
            return map;
        } catch (MailException e) {
            e.printStackTrace();
        }
        map.put("code",-1);
        return map;
    }

    ///contract/delContract 删除一个合同
    @RequestMapping("/contract/delContract")
    @ResponseBody
    public boolean delContract(@RequestParam("contractId") Integer contractId){
        System.out.println(contractId+"<===contractId");
        return deployServiceImpl.delDeploy(contractId);
    }
}
